﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Objects;
using System.Web.Security;
using BookStore.VC15;

namespace BookStore
{
    public partial class NXB_ThemSach : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            addbookTable.Enabled = true;
            MembershipUser obj = Membership.GetUser();
            if (obj == null)
            {
                LbWelcome.Text = "Chào mừng nhà xuất bản <br/>Bạn vui lòng <a href=\"NXB_DangNhap.aspx\">đăng nhập</a> để thực hiện chức năng này";
                addbookTable.Enabled = false;
                return;
            }
            using (var context = new BookStoreEntities())
            {
                //show ten nxb
                Guid uId = (Guid)obj.ProviderUserKey;//user id
                //hien thi ten nha xuat ban
                var sQuery = "select value nxb from BookStoreEntities.NhaXuatBans as nxb"
                + " where nxb.UserID=@UserID";

                ObjectQuery<NhaXuatBan> khs = context.CreateQuery<NhaXuatBan>(sQuery);
                khs.Parameters.Add(new ObjectParameter("UserID", uId));
                //KhachHang k = new KhachHang();
                List<NhaXuatBan> nxb = khs.ToList<NhaXuatBan>();
                if (nxb.Count > 0)
                {
                    LbNhaXuatBan.Text = nxb[0].TenNXB;
                    DropdownDS.SelectParameters["maNXB"].DefaultValue = "" + nxb[0].MaNXB; 
                }
                else
                {
                    LbWelcome.Text = "Vui lòng đăng nhập bằng tài khoản nhà xuất bản được cấp";
                    addbookTable.Enabled = false;
                }
            }
        }

        protected void btRefresh_Click(object sender, EventArgs e)
        {
            String smaLoaiSach = DropDownLoaiSach.SelectedValue;
            dtSach.SelectParameters["MaLoaiSach"].DefaultValue = smaLoaiSach;
            
            DropDownTenSach.DataBind();
        }

        protected void btCapNhat_Click(object sender, EventArgs e)
        {
            int soLuong = -1;
            int maSach = -1;
             
            try
            {
                soLuong = Convert.ToInt16(txtSoLuong.Text);
            }
            catch
            {
                soLuong = -1;
            }
            //kiem tra so luong hop le
            if (soLuong < 0)
            {
                lbRes.Text = "Số lượng nhập vào không hợp lệ";
                Page_Load(sender, e);
                return;
            }

            ///kiem tra ma sach hop le 
             try
            {
                maSach = Convert.ToInt16(DropDownTenSach.SelectedValue);
            }
            catch
            {
                maSach = -1;
            }
             if (maSach < 0)
             {
                 lbRes.Text = "Vui lòng chọn sách cần update";
                 Page_Load(sender, e);
                 return;
             }

            ///////////////////////////////////////////////////////////////////////////
            ////////////////////////////////////////////////////////////////////
            //Calling WebServices
            //Static information of Receiver
            string ToAddress = "Nhóm Kinh Doanh 12";
            string ToProvinceID = "";

            //Getting parameters
            string FromProvinceID = ddlTinhThanh.SelectedValue;
            string FromAddress = From.Text;
            DateTime strNow = DateTime.Now;

            //Get Transport service name
            string TranSer = ddlTranService.SelectedValue;

            bool havingErrors = false;
            double price = 0;
            string transkey = "";
            string key;
            switch (TranSer)
            {
                case "VC15":
                    ToProvinceID = "HCM";
                    //Login
                    VC15.Webservice vc15 = new VC15.Webservice();
                    key = vc15.WSLogin("KD12-65", "KD12-65");
                    if (key == "-1")
                    {
                        havingErrors = true;
                        break;
                    }

                    //Get ShippingPrice
                    VC15.TransportType[] vc15TranType = vc15.GetTransportType();
                    price = vc15.ShippingPrice(FromProvinceID, ToProvinceID, vc15TranType[0].ID, DropDownTenSach.SelectedItem.Text + ": " + soLuong.ToString());
                    if (price <= 0)
                    {
                        havingErrors = true;
                        break;
                    }

                    //Place command
                    transkey = vc15.PlaceTransportCommand(key, FromProvinceID, ToProvinceID, FromAddress, ToAddress, vc15TranType[0].ID, DropDownTenSach.SelectedItem.Text + ": " + soLuong.ToString(), strNow.AddDays(2));
                    if (transkey == "0" || transkey == "-1")
                    {
                        havingErrors = true;
                        break;
                    }
                    break;
                case "VC07":
                    ToProvinceID = "5";
                    //Login
                    VC07.Service vc07 = new VC07.Service();
                    key = vc07.WSLogin("kd12", "kd12");
                    if (key == "-1")
                    {
                        havingErrors = true;
                        break;
                    }

                    //Get ShippingPrice
                    VC07.TransportType[] vc07TranType = vc07.GetTransportType();
                    price = vc07.ShippingPrice(FromProvinceID, ToProvinceID, vc07TranType[0].ID, DropDownTenSach.SelectedItem.Text + ": " + soLuong.ToString());
                    if (price <= 0)
                    {
                        havingErrors = true;
                        break;
                    }

                    //Place command
                    transkey = vc07.PlaceTransportCommand(key, FromProvinceID, ToProvinceID, FromAddress, ToAddress, vc07TranType[0].ID, DropDownTenSach.SelectedItem.Text + ": " + soLuong.ToString(), strNow.AddDays(2));
                    //transkey = vc07.PlaceTransportCommand(key, "1", "2", "123", "321", "1", "a : 1", strNow.AddDays(2));
                    if (transkey == "0" || transkey == "-1")
                    {
                        havingErrors = true;
                        break;
                    }
                    break;
                case "VC11":
                    ToProvinceID = "HCM";
                    //Login
                    VC11.Service1 vc11 = new VC11.Service1();
                    key = vc11.WSLogin("sv", "sv");
                    if (key == "-1")
                    {
                        havingErrors = true;
                        break;
                    }

                    //Get ShippingPrice
                    VC11.TransportType[] vc11TranType = vc11.GetTransportType();
                    price = vc11.ShippingPrice(FromProvinceID, ToProvinceID, vc11TranType[0].ID, DropDownTenSach.SelectedItem.Text + ": " + soLuong.ToString());
                    if (price <= 0)
                    {
                        havingErrors = true;
                        break;
                    }

                    //Place command
                    transkey = vc11.PlaceTransportCommand(key, FromProvinceID, ToProvinceID, FromAddress, ToAddress, vc11TranType[0].ID, DropDownTenSach.SelectedItem.Text + ": " + soLuong.ToString(), strNow.AddDays(2));
                    if (transkey == "0" || transkey == "-1")
                    {
                        havingErrors = true;
                        break;
                    }
                    break;
                case "VC13":
                    ToProvinceID = "5";
                    //Login
                    VC13.Service vc13 = new VC13.Service();
                    key = vc13.WSLogin("123", "123");
                    if (key == "-1")
                    {
                        havingErrors = true;
                        break;
                    }

                    //Get ShippingPrice
                    VC13.TransportType[] vc13TranType = vc13.GetTransportType();
                    price = vc13.ShippingPrice(FromProvinceID, ToProvinceID, vc13TranType[0].ID, DropDownTenSach.SelectedItem.Text + ": " + soLuong.ToString());
                    if (price <= 0)
                    {
                        havingErrors = true;
                        break;
                    }

                    //Place command
                    transkey = vc13.PlaceTransportCommand(key, FromProvinceID, ToProvinceID, FromAddress, ToAddress, vc13TranType[0].ID, DropDownTenSach.SelectedItem.Text + ": " + soLuong.ToString(), strNow.AddDays(2));
                    if (transkey == "0" || transkey == "-1")
                    {
                        havingErrors = true;
                        break;
                    }
                    break;
            }

            //End if have errors
            if (havingErrors == true)
            {
                lbRes.Text = "Có lỗi trong quá trình thêm sách. Vui lòng thử lại sau!";
                return;
            }

            using (var context = new BookStoreEntities())
            {
                Sach sach = (from s in context.Saches
                             where s.MaSach == maSach
                             select s).First();
                sach.SoLuong += soLuong;
                context.SaveChanges();
                lblTransportKey.Text = "Mã vận chuyển hoặc link theo dõi: <strong>" + transkey + "</strong>";
                lblTransportQuantity.Text = "Số lượng vận chuyển: <strong>" + soLuong.ToString() + "</strong>";
                lblTransportService.Text = "Dịch vụ vận chuyển: <strong>" + TranSer + "</strong>";
                lblPrice.Text = "Giá vận chuyển: <strong>" + price + "</strong>";
                lbRes.Text = "Thực hiện thành công!";
                Page_Load(sender, e);
            }
        }

        protected void btSLRefresh_Click(object sender, EventArgs e)
        {
            int maSach = -1;
            try
            {
                maSach = Convert.ToInt16(DropDownTenSach.SelectedValue);
            }
            catch
            {
                return;
            }

            using (var context = new BookStoreEntities())
            {
                Sach sach = (from s in context.Saches
                             where s.MaSach == maSach
                             select s).First();
                txtSoLuongHienCo.Text = sach.SoLuong.ToString();
            }
        }

        protected void btProvRefresh_Click(object sender, EventArgs e)
        {
            ddlTinhThanh.Items.Clear();
            string TranSer = ddlTranService.SelectedValue;
            switch (TranSer)
            {
                case "VC15":
                    VC15.Webservice vc15 = new VC15.Webservice();
                    string vc15Key = vc15.WSLogin("KD12-65", "KD12-65");
                    VC15.Province[] vc15provinces = vc15.GetProvinceName();
                    foreach (VC15.Province pro in vc15provinces)
                    {
                        ddlTinhThanh.Items.Add(new ListItem(pro.ProvinceName, pro.ID));
                    }
                    break;
                case "VC07":
                    VC07.Service vc07 = new VC07.Service();
                    string vc07Key = vc07.WSLogin("KD12", "KD12");
                    VC07.Province[] vc07provinces = vc07.GetProvinceName();
                    foreach (VC07.Province pro in vc07provinces)
                    {
                        ddlTinhThanh.Items.Add(new ListItem(pro.ProvinceName, pro.ID));
                    }
                    break;
                case "VC11":
                    VC11.Service1 vc11 = new VC11.Service1();
                    string vc11Key = vc11.WSLogin("sv", "sv");
                    VC11.Province[] vc11provinces = vc11.GetProvinceName();
                    foreach (VC11.Province pro in vc11provinces)
                    {
                        ddlTinhThanh.Items.Add(new ListItem(pro.ProvinceName, pro.ID));
                    }
                    break;
                case "VC13":
                    VC13.Service vc13 = new VC13.Service();
                    string vc13Key = vc13.WSLogin("123", "123");
                    VC13.Province[] vc13provinces = vc13.GetProvinceName();
                    foreach (VC13.Province pro in vc13provinces)
                    {
                        ddlTinhThanh.Items.Add(new ListItem(pro.ProvinceName, pro.ID));
                    }
                    break;
            }
        }
    }
}